﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Extensions;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using ConfExpert.Utility.Grid;

namespace Tailord
{
    public partial class MagazaSiparis : XForm
    {
        public MagazaSiparis()
        {
            InitializeComponent();
        }

        private void MagazaSiparis_Load(object sender, EventArgs e)
        {
            ShowOnlyBar();
            gvSiparis.Init("GetRetailOrders", WhoAmI);
            gvSiparis.AddDetailGrid(gvSiparisLines, "RetailOrderId");


            gvSiparisLines.SutunEkle("ToplamPSF", typeof(decimal), "BirimFiyat*Adet");
            gvSiparisLines.SutunEkle("IndirimTutari", typeof(decimal), "IndirimOrani*ToplamPSF/100");
            gvSiparisLines.SutunEkle("IndirimliTutar", typeof(decimal), "ToplamPSF-Isnull(IndirimTutari,0)");
            gvSiparisLines.SutunEkle("NetCiro", typeof(decimal), "IndirimliTutar/(1+KDV/100)");
            gvSiparisLines.SutunEkle("KdvTutar", typeof(decimal), "IndirimliTutar-NetCiro");
            gvSiparisLines.SutunEkle("ParaPuan", typeof(decimal), "IIF(Isnull(Puan,0) = 0, 0, NetCiro*10/100)");

            gvSiparis.SutunEkle("ToplamPSF", typeof(decimal), "SUM(Child({FK}).ToplamPSF)");
            gvSiparis.SutunEkle("IndirimTutari", typeof(decimal), "SUM(Child({FK}).IndirimTutari)");
            gvSiparis.SutunEkle("IndirimliTutar", typeof(decimal), "SUM(Child({FK}).IndirimliTutar)");
            gvSiparis.SutunEkle("NetCiro", typeof(decimal), "SUM(Child({FK}).NetCiro)");
            gvSiparis.SutunEkle("KdvTutar", typeof(decimal), "SUM(Child({FK}).KdvTutar)");
            gvSiparis.SutunEkle("ParaPuan", typeof(decimal), "SUM(Child({FK}).ParaPuan)");
            gvSiparis.SutunEkle("Bakiye", typeof(decimal), "IndirimliTutar - Isnull(Kapora,0) - Isnull(Odeme,0)");

            gvSiparisLines.MakeColumnInvisible("RetailOrderId", "RetailOrderKId", "KayitTarihi", "Puan");
            gvSiparis.MakeColumnInvisible("KayitTarihi");

            gvSiparis.MakeReadOnly();
            gvSiparisLines.MakeReadOnly();

            gvSiparisLines.SetCaptions("UrunId:Ürün", "BirimFiyat:BirimPSF");

            gvSiparis.FormatAsDate("SiparisTarihi", "FaturaTarihi");
            gvSiparisLines.FormatAsDate("TerminTarihi");

            gvSiparisLines.FormatAsNumber(2, "BirimFiyat", "ToplamPSF", "NetCiro", "KdvTutar", "IndirimTutari", "IndirimliTutar", "ParaPuan");
            gvSiparis.FormatAsNumber(2, "Odeme", "Kapora", "ToplamPSF", "NetCiro", "KdvTutar", "IndirimTutari", "IndirimliTutar", "ParaPuan");
            gvSiparis.Columns["Bakiye"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            gvSiparis.Columns["Bakiye"].DisplayFormat.FormatString = "#,##0.##";

            gvSiparisLines.AddSummary("NetCiro", "KdvTutar", "IndirimTutari", "IndirimliTutar", "ToplamPSF", "ParaPuan");
            gvSiparis.AddSummary("NetCiro", "KdvTutar", "IndirimTutari", "IndirimliTutar", "ToplamPSF", "Kapora", "Odeme", "ParaPuan", "Bakiye");


            gvSiparisLines.YanYanaGetir("Kategori:Servis");
            gvSiparisLines.YanYanaGetir("ToplamPSF:IndirimOrani");
            gvSiparisLines.YanYanaGetirCoklu("TeslimTarihi", "IndirimTutari:", "IndirimliTutar:", "KDV:", "KdvTutar:", "NetCiro:");
            
            GridManager gm = GridManager.GetManager(gvSiparis);

            RepositoryItemHyperLinkEdit riPOId = new RepositoryItemHyperLinkEdit();
            riPOId.SingleClick = true;
            gvSiparis.Columns["RetailOrderId"].ColumnEdit = riPOId;
            gvSiparis.RowCellClick += gvSiparis_RowCellClick;
            gvSiparis.CustomDrawCell += gvSiparis_CustomDrawCell;
            gvSiparis.MouseMove += gvSiparis_MouseMove;

            GridColumn gcFatura = new GridColumn();
            gcFatura.Name = "Fatura";
            gcFatura.Caption = "Ft";
            gcFatura.FieldName = "Fatura";
            gcFatura.ToolTip = "Fatura eklemek için tıklayınız.";
            gcFatura.VisibleIndex = 800;
            gvSiparis.Columns.Add(gcFatura);

            GridColumn gc = new GridColumn();
            gc.Name = "Remove";
            gc.Caption = "Sil";
            gc.FieldName = "Remove";
            gc.VisibleIndex = 900;
            gvSiparis.Columns.Add(gc);
            
            gvSiparis.BestFitColumns();
            gvSiparisLines.BestFitColumns();

            gvSiparis.YanYanaGetir("FaturaNo:Fatura");
            gvSiparis.YanYanaGetir("FaturaTarihi:FaturaNo");
            gvSiparis.Columns["Remove"].Width = gvSiparis.Columns["Fatura"].Width = 28;
        }


        void gvSiparis_MouseMove(object sender, MouseEventArgs e)
        {
            GridHitInfo pos = gvSiparis.CalcHitInfo(e.X, e.Y);
            if (pos.RowHandle > -1 && pos.Column != null)
            {
                if (pos.Column.Name == "Remove" || pos.Column.Name == "Fatura")
                    gcSiparis.Cursor = Cursors.Hand;
                else
                    gcSiparis.Cursor = Cursors.Default;
            }
        }

        void gvSiparis_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                int iconYIndent = (e.Bounds.Height - 16) / 2;
                switch (e.Column.FieldName)
                {                        
                    case "Remove":
                        e.Graphics.DrawImage(Tailord.Properties.Resources.delete, e.Bounds.X + 1, e.Bounds.Y + iconYIndent);
                        e.Handled = true;
                    break;
                    case "Fatura":
                        e.Graphics.DrawImage(Tailord.Properties.Resources.invoice_icon, e.Bounds.X + 1, e.Bounds.Y + iconYIndent);
                        e.Handled = true;
                    break;
                }
            }
        }

        void gvSiparis_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                int RetailOrderId = gvSiparis.GetRowCellValue(e.RowHandle, "RetailOrderId").ToIntZero();
                switch (e.Column.FieldName)
                {
                    case "RetailOrderId":
                        if (RetailOrderId > 0)
                            using (SiparisOlustur so = new SiparisOlustur(RetailOrderId))
                            {
                                DialogResult dr = so.ShowDialog();
                                if (dr == DialogResult.OK)
                                    bbiGuncelle.PerformClick();
                            }
                    break;
                    case "Remove":
                    if (XtraMessageBox.Show(RetailOrderId + " nolu siparişi silmek istediğinizden emin misiniz?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) 
                    {
                        ExecQuery("DelRetailOrder",RetailOrderId,WhoAmI);
                        bbiGuncelle.PerformClick();
                    }
                    break;
                    case "Fatura":
                        FaturaEkle fe = new FaturaEkle(RetailOrderId);
                        DialogResult drFatura = fe.ShowDialog();
                        if (drFatura == DialogResult.OK)
                        {
                            ExecQuery("AddRetailFaturaNo", RetailOrderId, fe.FaturaNo, fe.FaturaTarihi,WhoAmI); 
                            XtraMessageBox.Show("Bilgiler eklendi!");
                            bbiGuncelle.PerformClick();
                        }
                    break;
                }
            }   
        }

        private void bbiSiparisOlustur_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (SiparisOlustur so = new SiparisOlustur(-1))
            {
                DialogResult dr=so.ShowDialog();
                if (dr == DialogResult.OK)
                    bbiGuncelle.PerformClick();
            }
        }

        private void bbiGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvSiparis.VeriyiGuncelle("GetRetailOrders", WhoAmI);
        }
    }
}
